# -*- coding: utf-8 -*-
 
'''
Python多张图片转成一个PDF文件
'''
 
# 导入包
from PIL import Image
import os
 
# 定义方法
def combine_imgs_pdf(folder, pdfFile):
    '''合成文件夹下的所有图片为一个PDF'''
    print('转换开始')
    files = os.listdir(folder)
    png_files = []
    sources = []
    for file in files:
        # 支持png jpg jpeg 图片
        if 'png' in file or 'jpeg' in file or 'jpg' in file:
            png_files.append(folder + file)
            print(folder + file)
    png_files.sort()
    png_files.pop(0)
    for file in png_files:
        png_file = Image.open(file)
        if png_file.mode == "RGBA":
            png_file = png_file.convert("RGB")
        sources.append(png_file)
    # 输出PDF
    png_file.save(pdfFile, "pdf", 
        save_all=True, append_images=sources)
    print('转换完成：'+pdfFile)
 
if __name__ == "__main__":
 
    # 图片的目录位置
    folder = r"/Users/moss/Desktop/1517NEW/"
 
    # PDF保存位置
    pdfFile = r"/Users/moss/Desktop/1517NEW/1517NEW.pdf"
 
    # 执行转换
    combine_imgs_pdf(folder, pdfFile)